package com.coolw.common.api;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.dromara.hutool.core.text.StrUtil;

import java.io.Serializable;

/**
 * 返回响应基类
 *
 * @author coolw
 * @date 2024/2/27 10:26
 */
@Data
@NoArgsConstructor
public class BaseResult<T> implements Serializable {
    private static final long serialVersionUID = 6072474966713805681L;

    private Integer code;

    private T data;

    private String msg;

    public static <T> BaseResult<T> success() {
        return success(null);
    }

    public static <T> BaseResult<T> success(T data) {
        BaseResult<T> result = new BaseResult<>();
        result.setCode(0);
        result.setData(data);
        result.setMsg("success");
        return result;
    }

    public static <T> BaseResult<T> fail(String msg) {
        return fail(null, msg);
    }

    public static <T> BaseResult<T> fail(Integer code, String msg) {
        BaseResult<T> result = new BaseResult<>();
        result.setCode(code != null ? code : -1);
        result.setMsg(StrUtil.defaultIfBlank(msg, "fail"));
        return result;
    }

    public static <T> boolean checkIsFail(BaseResult<T> result) {
        return !checkIsSuccess(result);
    }

    public static <T> boolean checkIsSuccess(BaseResult<T> result) {
        return result != null && result.code == 0;
    }
}
